<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/25
 * Time: 19:28
 */

namespace App\Spider;


use App\Utility\Curl;
use EasySwoole\HttpClient\HttpClient;
use EasySwoole\MysqliPool\Mysql;

class GameMatchEvent
{
    static function consume($data)
    {
        if( empty($data['data']) ){
            return false;
        }
        $db = Mysql::defer('mysql');
        if( $data['type'] == 1 ){
            foreach ( $data['data'] as $k=>$v ){
                $mysqlData = [
                    'left_team'  => json_encode($v['left_team']),
                    'right_team' => json_encode($v['right_team']),
                    'league'     => json_encode($v['league']),
                    'start_time' => $v['start_time'],
                    'lol_pandascore_match_id' => $v['lol_pandascore_match_id'],
                    'odd_niugame'=> json_encode($v['odd_niugame']),
                    'odd_188'    => json_encode($v['odd_188']),
                    'odd_mansion'=> json_encode($v['odd_mansion']),
                    'odd_vpgame' => json_encode($v['odd_vpgame']),
                    'live_channels' => json_encode($v['live_channels']),
                    'right_lead_score' => $v['right_lead_score'],
                    'left_lead_score'  => $v['left_lead_score'],
                    'right_score'=> $v['right_score'],
                    'left_score' => $v['left_score'],
                    'dota2_matches'=> json_encode($v['dota2_matches']),
                    'state'      => $v['state'],
                    'odd_data'   => json_encode($v['odd_data']),
                    'live_match' => isset($v['live_match']) ? json_encode($v['live_match']):'[]',
                    '_id'        => $v['live_match']['_id'] ?? $v['_id'] ,
                    'category'   => $v['category'],
                    'round'      => $v['round']
                ];
                if( $v['category'] == 'dota2' ){
                    if(!empty($v['dota2_matches'])){
                        foreach ( $v['dota2_matches'] as $k1=>$v1 ){
                            RedisLogic::addConsume([
                                'type' => 2,
                                'game' => $v['category'],
                                'data' => $v1,
                            ]);
                        }
                    }

                }elseif( $v['category'] == 'lol' ){
                    RedisLogic::addConsume([
                        'type' => 2,
                        'game' => $v['category'],
                        'data' => $mysqlData['_id']
                    ]);
                }

                $count = $db->where('_id',$mysqlData['_id'])
                    ->where('category',$mysqlData['category'])->count('siam_game_competitions');
                if( $count > 0 ){
                    $db->where('_id',$mysqlData['_id'])
                        ->where('category',$mysqlData['category'])
                        ->update('siam_game_competitions',$mysqlData);
                }else{
                    $db->insert('siam_game_competitions',$mysqlData);
                }
            }
        }elseif($data['type'] == 2){
            if ($data['game'] == 'dota2'){
                $response = Curl::request('GET','https://v0.1zplay.com/api/dota2_match/'.$data['data']);
                $http_code = $response->getCurlInfo()['http_code'];
                if( $http_code == 200 ){
                    $detail = $response->__toString();
                    if( empty($detail) ) return "dota2:detail:empty";
                    $detail = json_decode($detail,true);
                    $mysqlData = [
                        '_id'       => $detail['_id'], //详情ID
                        'players'   => json_encode($detail['players']),//参赛者
                        'dire_picks'=> json_encode($detail['dire_picks']),//dire夜魔阵营选择
                        'dire_bans' => json_encode($detail['dire_bans']),//dire夜魔阵营ban
                        'radiant_bans' => json_encode($detail['radiant_bans']),//radiant天辉阵营ban
                        'radiant_picks'=> json_encode($detail['radiant_picks']),//radiant天辉阵营选择
                        'dire'      => json_encode($detail['dire']),//夜魔
                        'radiant'   => json_encode($detail['radiant']),//天辉
                        'graph_gold'=> json_encode($detail['graph_gold']),
                        'graph_xp'  => json_encode($detail['graph_xp']),
                        'radiant_gold_lead' => $detail['radiant_gold_lead'],//夜魔领先
                        'ten_kills' => $detail['ten_kills'],
                        'first_tower'=> $detail['first_tower'],
                        'first_blood'=> $detail['first_blood'],
                        'win_side'  => $detail['win_side'],
                        'finished'  => $detail['finished'],
                        'game_time' => $detail['game_time'],
                        'start_time'=> $detail['start_time'],
                        'league_id' => $detail['league_id'],
                        'dire_team_id' => $detail['dire_team_id'],
                        'radiant_team_id'=> $detail['radiant_team_id'],
                        'dire_team' => json_encode($detail['dire_team']),
                        'radiant_team' => json_encode($detail['radiant_team'])
                    ];
                    $count = $db->where('_id',$detail['_id'])->count('siam_dota2_gamedetail');
                    if( $count == 0 ){
                        $res = $db->insert('siam_dota2_gamedetail',$mysqlData);
                    }elseif ( $count > 0 && $detail['finished'] == false ){
                        $res = $db->where('_id',$detail['_id'])->update('siam_dota2_gamedetail',$mysqlData);
                    }else{
                        $res = true;
                    }
                    return "siam_dota2_gamedetail:{$res}";
                }
            }elseif ( $data['game'] == 'lol' ){
                $response = Curl::request('GET','https://v0.1zplay.com/api/lol_match/'.$data['data']);
                $http_code = $response->getCurlInfo()['http_code'];
                if( $http_code == 200 ){
                    $detail = $response->__toString();
                    if( empty($detail) ) return "lol:detail:empty";
                    $detail = json_decode($detail,true);
                    $mysqlData = [
                        '_id'           => $detail['_id'],
                        'update_time'   => $detail['update_time'],//修改时间
                        'create_time'   => $detail['create_time'],//创建时间
                        'start_time'    => $detail['start_time'] ?? '',//开始时间
                        'red'           => json_encode($detail['red']),//红色队伍
                        'blue'          => json_encode($detail['blue']),//蓝色队伍
                        'position'      => $detail['position'],
                        'lol_pandascore_match_id' => $detail['lol_pandascore_match_id'],
                        'lol_pandascore_game_id'  => $detail['lol_pandascore_game_id'],
                        'graph_gold'    => json_encode($detail['graph_gold']),
                        'blue_gold_lead'=> json_encode($detail['blue_gold_lead']),
                        'five_kills_other_score' => $detail['five_kills_other_score'],
                        'five_kills'    => $detail['five_kills'],//获得五杀
                        'first_tower'   => $detail['first_tower'],//一塔
                        'first_blood'   => $detail['first_blood'],//一血
                        'win_side'      => $detail['win_side'],//获胜方
                        'valid'         => $detail['valid'],//是否有效
                        'finished'      => $detail['finished'],//是否结束
                        'game_time'     => $detail['game_time'],//游戏时间
                        'blue_team'     => json_encode($detail['blue_team']),
                        'red_team'      => json_encode($detail['red_team']),
                        'category'      => $detail['category']
                    ];
                    $count = $db->where('_id',$detail['_id'])->count('siam_lol_gamedetail');
                    if( $count == 0 ){
                        $res = $db->insert('siam_lol_gamedetail',$mysqlData);
                    }elseif ( $count > 0 && $detail['finished'] == false ){
                        $res = $db->where('_id',$detail['_id'])->update('siam_lol_gamedetail',$mysqlData);
                    }else{
                        $res = true;
                    }

                    return "siam_lol_gamedetail:{$res}";
                }
            }

        }


//        $db = Mysql::defer('mysql');
//        if( $data['type'] == 'update' ){
//            $db->where('_id',$data['data']['_id'])
//                ->where('category',$data['data']['category'])->update('siam_game_competitions',$data['data']);
//            RedisLogic::addConsume($data);
//        }elseif ( $data['type'] == 'insert' ){
//            $db->insert('siam_game_competitions',$data['data']);
//            RedisLogic::addConsume($data);
//        }
    }
    static function produce($data)
    {
        RedisLogic::addConsume($data);
        return true;
    }
}